package com.ttj.service.impl;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ttj.domain.oracle.BasService;
import com.ttj.domain.oracle.ConfigCustomerExt;
import com.ttj.domain.oracle.OrderInstock;
import com.ttj.domain.oracle.OrderInstockDetail;
import com.ttj.domain.oracle.OrderRec;
import com.ttj.domain.oracle.OrderRecDetail;
import com.ttj.domain.oracle.OrderSell;
import com.ttj.domain.oracle.OrderSellDetail;
import com.ttj.domain.oracle.SysconfRecCategory;
import com.ttj.domain.oracle.SysconfService;
import com.ttj.domain.oracle.WebUser;
import com.ttj.mapper.oracle.BasServiceMapper;
import com.ttj.mapper.oracle.ConfigCustomerExtMapper;
import com.ttj.mapper.oracle.OrderInstockDetailMapper;
import com.ttj.mapper.oracle.OrderInstockMapper;
import com.ttj.mapper.oracle.OrderOutstockMapper;
import com.ttj.mapper.oracle.OrderRecDetailMapper;
import com.ttj.mapper.oracle.OrderRecMapper;
import com.ttj.mapper.oracle.OrderSellDetailMapper;
import com.ttj.mapper.oracle.OrderSellMapper;
import com.ttj.mapper.oracle.SysconfRecCategoryMapper;
import com.ttj.mapper.oracle.SysconfServiceMapper;
import com.ttj.mapper.oracle.WebEvaluateBUserMapper;
import com.ttj.mapper.oracle.WebUserMapper;
import com.ttj.service.serviceGroupService;
import com.ttj.util.InitTimeUtil;
import com.ttj.vo.BaseRes;
@Service
public class serviceGroupServiceImpl implements serviceGroupService {
	@Resource
	private OrderRecMapper mOrderRecMapper;
	@Resource
	private OrderSellMapper mOrderSellMapper;
	@Resource
	private SysconfRecCategoryMapper mSysconfRecCategoryMapper;
	@Resource
	private ConfigCustomerExtMapper mConfigCustomerExtMapper;
	@Resource
	private OrderOutstockMapper mOrderOutstockMapper;
	@Resource
	private WebUserMapper mWebUserMapper;
	@Resource
	private SysconfServiceMapper mSysconfServiceMapper;
	@Resource
	private OrderInstockMapper mOrderInstockMapper;
	@Resource
	private OrderInstockDetailMapper mOrderInstockDetailMapper;
	@Resource
	private OrderRecDetailMapper mOrderRecDetailMapper;
	@Resource
	private OrderSellDetailMapper mOrderSellDetailMapper;
	@Resource
	private BasServiceMapper mBasServiceMapper;
	@Resource
	private WebEvaluateBUserMapper mWebEvaluateBUserMapper;
	
	@Override
	/**
	 * 展示服务组未响应数量
	 */
	public BaseRes<SysconfService> getUnResponseCount(String admin) {
		// TODO Auto-generated method stub
		BaseRes<SysconfService> brs = new BaseRes<SysconfService>();
		List<SysconfService> sysconfService = mSysconfServiceMapper.findSysconfServiceByUsername(admin);
		brs.setData(sysconfService.get(0));
		int unResponseCount = mBasServiceMapper.getUnResponseCount(sysconfService.get(0).getId()+"");
		brs.setMsg(unResponseCount+"");
		return brs;
	}
	/**
	 * 用于展示服务组未回复、已查看、已完成列表
	 */
	@Override
	public BaseRes<Map> showServiceGroupList(String sid) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		BaseRes<Map> brs = new BaseRes<Map>();
		map.put("sid", sid);
		map.put("oStauts", 0);
		map.put("rStatus", 0);
		List<BasService> noReplyList = mBasServiceMapper.getServiceReminderList(map);//未回复列表
		for(BasService nl:noReplyList){
			nl.setInDate(sdf.format(nl.getIndate()));
			nl.setExpectTime(sdf.format(nl.getExpTime()));
		}
		map.put("oStauts", 0);
		map.put("rStatus", 1);
		map.put("anotherOStatus", 1);
		List<BasService> repliedList = mBasServiceMapper.getServiceReminderList(map);//已回复列表
		for(BasService rl:repliedList){
			rl.setReplyDate(sdf.format(rl.getrDate()));
			rl.setExpectTime(sdf.format(rl.getExpTime()));
		}
		map.put("oStauts", 2);
		map.remove("anotherOStatus");
		Calendar cal = Calendar.getInstance(); 
		cal.add(Calendar.MONTH, -5);
		String startDate = InitTimeUtil.getStartDaytime(cal);
		map.put("startDate", startDate);
		List<BasService> completedList = mBasServiceMapper.getServiceReminderList(map);//已完成列表
		for(BasService cl:completedList){
			cl.setCompleteTime(sdf.format(cl.getModifTime()));
		}
		map1.put("noReplyList", noReplyList);
		map1.put("repliedList", repliedList);
		map1.put("completedList", completedList);
		brs.setData(map1);
		return brs;
	}
	
	@Override
	/**
	 * 根据订单号查询未查看、已回复详情
	 */
	public BaseRes<BasService> getByOrderId(String orderId,String type) {
		// TODO Auto-generated method stub
		BaseRes<BasService> brs = new BaseRes<BasService>();
		List<BasService> basServiceList = mBasServiceMapper.getByOrderId(orderId);
		BasService showOne = basServiceList.get(0);
		if(type.equals("2")){
			List<OrderSell> list = mOrderSellMapper.getByOrderId(orderId);
			OrderSell orderSell = list.get(0);
			if(orderSell.getInvoiceDeliverMethod()==1){
				showOne.setInvoiceDeliverMethod("随货");
			}
			if(orderSell.getInvoiceDeliverMethod()==2){
				showOne.setInvoiceDeliverMethod("另邮寄");
			}
			showOne.setUncode(mOrderOutstockMapper.getUnCodeByOrderId(orderId));
			List<OrderSellDetail> orderDetail = mOrderSellDetailMapper.getDetailByOrderId(orderId);
			for(OrderSellDetail oo:orderDetail){
				if(oo.getgUnit()==1){
					oo.setgUnitString("个");
				}
				if(oo.getgUnit()==0){
					oo.setgUnitString("公斤");
				}
			}
			showOne.setOrderDetail(orderDetail);
		}
		brs.setData(showOne);
		return brs;
	}
	/**
	 * 用于展示服务组已完成详情
	 */
	@Override
	public BaseRes<BasService> getCompleteByOrderId(String orderId) {
		// TODO Auto-generated method stub
		BaseRes<BasService> brs = new BaseRes<BasService>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<BasService> basServiceList = mBasServiceMapper.getByOrderId(orderId);
		BasService showOne = basServiceList.get(0);
		int totalMoney = 0;
		if(showOne.getOrderType()==1){
			List<OrderSell> list = mOrderSellMapper.getByOrderId(orderId);
			OrderSell orderSell = list.get(0);//completeTime  payMethod
			if(orderSell.getPayMethod()==1){
				showOne.setPayMethod("现金");
			}
			if(orderSell.getPayMethod()==2){
				showOne.setPayMethod("转账");
			}
			if(orderSell.getPayMethod()==3){
				showOne.setPayMethod("支票");
			}
			if(orderSell.getPayMethod()==4){
				showOne.setPayMethod("存账");
			}
			showOne.setCompleteTime(sdf.format(orderSell.getlCTime()));
			List<OrderSellDetail> orderDetail = mOrderSellDetailMapper.getDetailByOrderId(orderId);
			for(OrderSellDetail oo:orderDetail){
				if(oo.getgUnit()==0){
					oo.setgUnitString("个");
				}
				if(oo.getgUnit()==1){
					oo.setgUnitString("公斤");
				}
				totalMoney+=oo.getTprice();
			}
			showOne.setOrderDetail(orderDetail);
			showOne.setTotalMoney(totalMoney);
		}
		if(showOne.getOrderType()==2){
			List<OrderRec> list = mOrderRecMapper.getByOrderId(orderId);
			OrderRec orderRec = list.get(0);//completeTime  payMethod
			if(orderRec.getPayMethod()==1){
				showOne.setPayMethod("现金");
			}
			if(orderRec.getPayMethod()==2){
				showOne.setPayMethod("转账");
			}
			if(orderRec.getPayMethod()==3){
				showOne.setPayMethod("支票");
			}
			if(orderRec.getPayMethod()==5){
				showOne.setPayMethod("存账");
			}
			showOne.setCompleteTime(sdf.format(orderRec.getlCTime()));
			List<OrderRecDetail> orderDetail = mOrderRecDetailMapper.getDetailByOrderId(orderId);
			for(OrderRecDetail oo:orderDetail){
				if(oo.getgUnit()==1){
					oo.setgUnitString("个");
				}
				if(oo.getgUnit()==0){
					oo.setgUnitString("公斤");
					oo.setrNum(oo.getrNum()/1000);
				}
				totalMoney+=oo.getfPrice()/100;
			}
			showOne.setOrderRecDetail(orderDetail);
			showOne.setTotalMoney(totalMoney);
		}
		brs.setData(showOne);
		return brs;
	}
	/**
	 * 用于服务组点击接受
	 */
	@Override
	public BaseRes<BasService> recieveMsg(String orderId, String userName,String orderType) {
		// TODO Auto-generated method stub
		BaseRes<BasService> brs = new BaseRes<BasService>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		String nowTime = sdf.format(now);
		int a = 0;
		int b = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("nowTime", nowTime);
		map.put("userName", userName);
		map.put("orderId", orderId);
		if(orderType.equals("1")){
			a = mBasServiceMapper.recieveMsg(map);
			b = mOrderSellMapper.recieveMsg(map);
		}
		if(orderType.equals("2")){
			a = mBasServiceMapper.recieveMsg(map);
			b = mOrderRecMapper.recieveMsg(map);
		}
		String c = a*b + "";
		brs.setMsg(c);
		return brs;
	}
	/**
	 * 用于服务组提货完成
	 */
	@Override
	public BaseRes<BasService> pickUpGoods(String orderId, String userName) {
		// TODO Auto-generated method stub
		BaseRes<BasService> brs = new BaseRes<BasService>();
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		String nowTime = sdf.format(now);
		map.put("nowTime", nowTime);
		map.put("userName", userName);
		map.put("orderId", orderId);
		int a = mOrderOutstockMapper.pickUpGoods(map);
		int b = mOrderSellMapper.pickUpGoods(map);
		int d = mBasServiceMapper.pickUpGoods(map);
		String c = a*b*d + "";
		brs.setMsg(c);
		return brs;
	}
	/**
	 * 用于服务组拉货完成
	 */
	@Override
	public BaseRes<BasService> deliveryComplete(String orderId, String userName) {
		// TODO Auto-generated method stub
		BaseRes<BasService> brs = new BaseRes<BasService>();
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		String nowTime = sdf.format(now);
		map.put("nowTime", nowTime);
		map.put("userName", userName);
		map.put("orderId", orderId);
		int a = mOrderOutstockMapper.deliveryComplete(map);
		int b = mOrderSellMapper.deliveryComplete(map);
		int d = mBasServiceMapper.deliveryComplete(map);
		String c = a*b*d + "";
		brs.setMsg(c);
		return brs;
	}
	/**
	 * 用于展示服务组拉货列表
	 */
	@Override
	public BaseRes<Map> showServicePullGoodsList(String sid) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		BaseRes<Map> brs = new BaseRes<Map>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		List<OrderInstock> waitingPull = mOrderInstockMapper.showServiceWaitingPullList(sid);//待拉货
		for(OrderInstock cc:waitingPull){
			cc.setODateStr(sdf.format(cc.getoIndate()));
			System.out.println(sdf.format(cc.getoIndate()));
		}
		map.put("sid", sid);
		map.put("oStatus", 3);
		map.put("lType", 2);
		List<OrderInstock> completePull = mOrderInstockMapper.showServicePullGoodsList(map);//已拉货
		for(OrderInstock cc:completePull){
			cc.setDeliveryDateStr(sdf.format(cc.getDeliveryDate()));
		}
		map.put("oStatus", 5);
		List<OrderInstock> hangingPull = mOrderInstockMapper.showServicePullGoodsList(map);//挂起
		for(OrderInstock cc:hangingPull){
			cc.setHangingDate(sdf.format(cc.getHangDate()));
		}
		Calendar cal = Calendar.getInstance(); 
		cal.add(Calendar.MONTH, -5);
		String startTime = InitTimeUtil.getStartDaytime(cal);
		map.put("startTime", startTime);
		map.put("oStatus", 4);
		List<OrderInstock> validatePull = mOrderInstockMapper.showServicePullGoodsList(map);//已验收
		for(OrderInstock cc:validatePull){
			cc.setValiDate(sdf.format(cc.getValdateDate()));
		}
		map1.put("waitingPull", waitingPull);//待拉货列表
		map1.put("completePull", completePull);//已拉货列表
		map1.put("hangingPull", hangingPull);//挂起列表
		map1.put("validatePull", validatePull);//已验收列表
		brs.setData(map1);
		return brs;
	}
	/**
	 * 用于展示服务组拉货详情
	 */
	@Override
	public BaseRes<OrderInstock> showServicePullGoodsDetail(String orderId,
			String type) {
		// TODO Auto-generated method stub
		BaseRes<OrderInstock> brs = new BaseRes<OrderInstock>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<OrderInstock> serviceInstock = mOrderInstockMapper.getServiceDetailByorderId(orderId);
		String orderIds = serviceInstock.get(0).getOrderIds();
		if(type.equals("1")){
			serviceInstock.get(0).setODateStr(sdf.format(serviceInstock.get(0).getoIndate()));
		}
		if(type.equals("2")){
			serviceInstock.get(0).setDeliveryDateStr(sdf.format(serviceInstock.get(0).getDeliveryDate()));
		}
		if(type.equals("3")){
			serviceInstock.get(0).setValiDate(sdf.format(serviceInstock.get(0).getValdateDate()));
		}
		if(type.equals("4")){
			serviceInstock.get(0).setHangingDate(sdf.format(serviceInstock.get(0).getHangDate()));
		}
		System.out.println(orderIds);
		List<OrderRec> customers = mOrderRecMapper.getCustomerByOrderIds(orderIds);
		System.out.println(customers.toString());
		serviceInstock.get(0).setCustomers(customers);
		List<OrderInstockDetail> details = mOrderInstockDetailMapper.getOneByOrderId(orderId);
		for(OrderInstockDetail od:details){
			if(od.getUnit().equals("1")){
				od.setUnit("个");
			}else{
				od.setUnit("公斤");
			}
			od.setWeight(od.getWeight()/1000);
		}
		serviceInstock.get(0).setOrderListDetail(details);
		brs.setData(serviceInstock.get(0));
		return brs;
	}
	/**
	 * 用于服务组合并拉货
	 */
	@Override
	public BaseRes<OrderInstock> servicePullMerge(String orderIds) {
		// TODO Auto-generated method stub
		BaseRes<OrderInstock> brs = new BaseRes<OrderInstock>();
		List<OrderInstockDetail> searchForMergeOrders = mOrderInstockDetailMapper.searchForMergeOrders(orderIds);
		
		List<OrderInstock> selectByOrderIds = new ArrayList<OrderInstock>();
		OrderInstock newOrderInstock = new OrderInstock();
		Date now = new Date();
		double orderMoney1 = 0;
		double orderMoney5 = 0;
		String newOrderIds = "";
		String[] split = orderIds.split(",");
		for(int i=0;i<split.length;i++){
			OrderInstock orderInstock = mOrderInstockMapper.selectByOrderIds(split[i]).get(0);
			selectByOrderIds.add(orderInstock);
			mOrderInstockDetailMapper.deleteByOrderId(orderInstock.getOrderId()+"");
			mOrderInstockMapper.deleteByOrderIds(split[i]);
		}
		newOrderInstock.setInId(mOrderInstockMapper.findMaxInId()+1);
		newOrderInstock.setOrderId(mOrderInstockMapper.findMaxOrderId()+1);
		newOrderInstock.setoStatus(3);
		newOrderInstock.setType(1);
		newOrderInstock.setlType(2);
		newOrderInstock.setRate(0.17);
		newOrderInstock.setfStatus(0);
		newOrderInstock.setoIndate(now);
		newOrderInstock.setDeliveryDate(now);
		newOrderInstock.setModifTime(now);
		newOrderInstock.setValdateDate(now);
		newOrderInstock.setCreater(selectByOrderIds.get(0).getCreater());
		newOrderInstock.setSid(selectByOrderIds.get(0).getSid());
		for(OrderInstock ss:selectByOrderIds){
			orderMoney1+=ss.getOrderMoney1();
			orderMoney5+=ss.getOrderMoney5();
			newOrderIds=newOrderIds+","+ss.getOrderIds();
		}
		newOrderInstock.setOrderMoney1(orderMoney1);
		newOrderInstock.setOrderMoney5(orderMoney5);
		newOrderInstock.setOrderIds(newOrderIds.substring(1));
		System.out.println(newOrderInstock.toString());
		int result1 = mOrderInstockMapper.createNewOrder(newOrderInstock);
		OrderInstockDetail orderInstockDetail = new OrderInstockDetail();
		for(OrderInstockDetail ff:searchForMergeOrders){
			orderInstockDetail.setOrderId(newOrderInstock.getOrderId());
			orderInstockDetail.setgId(ff.getgId());
			orderInstockDetail.setNum(ff.getTotalNum());
			orderInstockDetail.setInId(newOrderInstock.getInId());
			orderInstockDetail.setoIndate(now);
			orderInstockDetail.setWeight(ff.getTotalWeight()+0.0);
			mOrderInstockDetailMapper.createNewOrderDetail(orderInstockDetail);
		}
		brs.setCode(result1);
		return brs;
	}
	
	@Override
	/**
	 * 用于展示服务组工作日志（提取条件部分）
	 */
	public BaseRes<Map> showServiceDailyLog(String searchTimeType,String bUser, String startDate, String endDate) {
		BaseRes<Map> brs = new BaseRes<Map>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		System.out.println("searchTimeType:"+searchTimeType+"  bUser:"+bUser+"  startDate:"+startDate+"  endDate:"+endDate);
		condition.put("bUser", bUser);
		if(startDate==null){
			System.out.println("empty");
		}else if(startDate.trim().equals("")){
			System.out.println("another empty");
		}else{
			System.out.println("not empty");
		}
		if((!startDate.trim().equals(""))&&(!endDate.trim().equals(""))){//按照时间搜索
			condition.put("startTime", startDate);
			condition.put("endTime", endDate);
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(searchTimeType.equals("1")){//搜索当日的数据
				Calendar cal = Calendar.getInstance(); 
				String endTime = sdf.format(cal.getTime());
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
			}else if(searchTimeType.equals("2")){//搜索当周的数据
				Calendar cal = Calendar.getInstance();
				String endTime = sdf.format(cal.getTime());
				cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-cal.get(Calendar.DAY_OF_WEEK)+1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
				
			}else if(searchTimeType.equals("3")){//搜索当月的数据
				Calendar cal = Calendar.getInstance();
				String endTime = sdf.format(cal.getTime());
				cal.set(Calendar.DATE, 1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
				
			}else if(searchTimeType.equals("4")){//搜索昨日的数据
				Calendar cal = Calendar.getInstance(); 
				String endTime = InitTimeUtil.getStartDaytime(cal);
				cal.add(Calendar.DATE, -1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
			}else if(searchTimeType.equals("5")){//搜索上周的数据
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.DATE, -7);
				cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-cal.get(Calendar.DAY_OF_WEEK)+1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				cal.add(Calendar.DATE, 7);
				String endTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
			}else{//搜索上个月的数据
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.MONTH, -1);
				cal.set(Calendar.DATE, 1);
				String startTime = InitTimeUtil.getStartDaytime(cal);
				cal.add(Calendar.MONTH, 1);
				String endTime = InitTimeUtil.getStartDaytime(cal);
				condition.put("startTime", startTime);
				condition.put("endTime", endTime);
				System.out.println(startTime+"%%%"+endTime);
			}
		}
		map = showServiceDailyLogByTime(condition);
		brs.setData(map);
		return brs;
	}
	@Override
	/**
	 * 用于展示服务组工作日志（数据库查询部分）
	 */
	public Map<String, Object> showServiceDailyLogByTime(Map<String, Object> condition) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> map = new HashMap<String, Object>();
		double totalDeposit = mOrderRecMapper.countServiceRecDeposit(condition);
		int recTimes = mOrderRecMapper.countServiceRecTimes(condition);
		int welldownTimes = mWebEvaluateBUserMapper.countPraiseTimes(condition);
		int badReviewTimes = mWebEvaluateBUserMapper.countbadReviewTimes(condition);
		int sumServiceTotalRecMoney = mOrderRecMapper.SumServiceTotalRecMoney(condition);
		double sumServiceTotalSellMoney = mOrderSellMapper.SumServiceTotalSellMoney(condition);
		double sumInstockMoney = mOrderInstockMapper.sumInstockMoney(condition);
		List<OrderInstockDetail> searchInstockForDailyLog = mOrderInstockDetailMapper.searchForDailyLog(condition);
		List<OrderRecDetail> searchRecForDailyLog = mOrderRecDetailMapper.searchForDailyLog(condition);
//		List<OrderInstock> acceptList = mOrderInstockMapper.getAcceptList(condition);
//		List<OrderInstock> delayList = new ArrayList<OrderInstock>();
//		int delayNum = 0;
//		for(OrderInstock ac:acceptList){
//			if(!sdf.format(ac.getDeliveryDate()).equals(sdf.format(ac.getValdateDate()))){
//				delayNum++;
//				ac.setValiDate(sdf.format(ac.getValdateDate()));
//				delayList.add(ac);
//			}
//		}
//		List<OrderInstock> hangingList = mOrderInstockMapper.getHangingList(condition);
//		for(OrderInstock hh:hangingList){
//			System.out.println(hh.toString());
//		}
//		List<OrderInstockDetail> countAcceptedDetails = mOrderInstockDetailMapper.countAcceptedDetails(condition);
//		List<OrderInstockDetail> showCountAcceptedDetails = new ArrayList<OrderInstockDetail>();
//		for(OrderInstockDetail cad:countAcceptedDetails){
//			if(cad.getTotalWeight()!=0){
//				if(cad.getUnit().equals("1")){
//					cad.setUnit("个");
//				}else{
//					cad.setUnit("公斤");
//				}
//				showCountAcceptedDetails.add(cad);
//			}
//		}
//		List<OrderInstockDetail> countHangingDetails = mOrderInstockDetailMapper.countHangingDetails(condition);
//		List<OrderInstockDetail> showCountHangingDetails = new ArrayList<OrderInstockDetail>();
//		for(OrderInstockDetail cad:countHangingDetails){
//			if(cad.getTotalWeight()!=0){
//				if(cad.getUnit().equals("1")){
//					cad.setUnit("个");
//				}else{
//					cad.setUnit("公斤");
//				}
//				showCountHangingDetails.add(cad);
//			}
//		}
		map.put("totalDeposit", totalDeposit);//工作日志绿色存款
		map.put("recTimes", recTimes);//工作日志统计回收次数
		map.put("welldownTimes", welldownTimes);//好评次数
		map.put("badReviewTimes", badReviewTimes);//差评次数
		map.put("sumServiceTotalRecMoney", sumServiceTotalRecMoney);//回收金额
		map.put("sumServiceTotalSellMoney", sumServiceTotalSellMoney);//销售金额
		map.put("sumInstockMoney", sumInstockMoney);//销售金额
		map.put("searchInstockForDailyLog", searchInstockForDailyLog);//验收明细
		map.put("searchRecForDailyLog", searchRecForDailyLog);//回收明细
		return map;
	}
	/**
	 * 用于服务组查询条形码是否被绑定
	 */
	@Override
	public BaseRes<WebUser> checkBarcode(String barcode) {
		// TODO Auto-generated method stub
		System.out.println(barcode+"service");
		BaseRes<WebUser> brs = new BaseRes<WebUser>();
		int result = mWebUserMapper.checkBarcode(barcode);
		System.out.println(result);
		brs.setCode(result);
		return brs;
	}
	/**
	 * 用于服务组绑定条形码
	 */
	@Override
	public BaseRes<WebUser> bandingBarcode(String barcode, String cusername) {
		// TODO Auto-generated method stub
		BaseRes<WebUser> brs = new BaseRes<WebUser>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("barcode", barcode);
		map.put("cusername", cusername);
		int result = mWebUserMapper.bandingBarcode(map);
		brs.setCode(result);
		return brs;
	}
	/**
	 * 用于服务组获取回收物品列表
	 */
	@Override
	public BaseRes<List> getRecCategoryList() {
		// TODO Auto-generated method stub
		BaseRes<List> brs = new BaseRes<List>();
		List<SysconfRecCategory> recCategoryList = mSysconfRecCategoryMapper.getRecCategoryList();
		brs.setData(recCategoryList);
		return brs;
	}
	
	/**
	 * 用于服务组创建匿名回收
	 */
	@Override
	public BaseRes<OrderRec> createAnonymousOrderRec(String orderRecStr,
			String recCategoryDetailListStr) {
		BaseRes<OrderRec> brs = new BaseRes<OrderRec>();
		Date now = new Date();
		Gson gson = new Gson();
		OrderRec orderRec = gson.fromJson(orderRecStr, OrderRec.class);
		Type listType = new TypeToken<List<OrderRecDetail>>(){}.getType();
		List<OrderRecDetail> fromJson = gson.fromJson(recCategoryDetailListStr,listType);
		int inId = mOrderRecMapper.findMaxId()+1;
		long orderId = mOrderRecMapper.findMaxOrderId()+1;
		orderRec.setId(inId);
		orderRec.setOrderId(orderId);
		orderRec.setCid(0);
		orderRec.setCextId(0);
		orderRec.setStype(1);
		double totalPrice = 0;
		for(OrderRecDetail ff:fromJson){
			totalPrice += ff.getfPrice();
		}
		orderRec.setPrice(totalPrice);
		orderRec.setPayMethod(1);
		orderRec.setoStatus(4);
		orderRec.setoIndate(now);
		orderRec.setfStatus(1);
		orderRec.setfJsTime(now);
		orderRec.setfJAuthor(orderRec.getoAuthor());
		orderRec.setfJMoney(totalPrice);
		orderRec.setOrderMoney(totalPrice);
		orderRec.setfJTime(now);
		orderRec.setlCTime(now);
		orderRec.setlAuthor(orderRec.getoAuthor());
		orderRec.setCommitType(1);
		orderRec.setsType(2);
		orderRec.setrDate(now);
		orderRec.setRperson(orderRec.getoAuthor());
		orderRec.setModifTime(now);
		orderRec.setIsRecComplete(1);
		int result = mOrderRecMapper.createOrderRec(orderRec);//匿名回收创建
		for(OrderRecDetail ff:fromJson){
			ff.setOrderId((int)orderId);
			mOrderRecDetailMapper.createRecDetail(ff);//匿名回收回收单详情创建
		}
		OrderInstock orderInstock = new OrderInstock();
		int findMaxInId = mOrderInstockMapper.findMaxInId()+1;
		int findMaxOrderId = mOrderInstockMapper.findMaxOrderId()+1;
		orderInstock.setInId(findMaxInId);
		orderInstock.setOrderId(findMaxOrderId);
		orderInstock.setoIndate(now);
		orderInstock.setCreater(orderRec.getoAuthor());
		orderInstock.setoStatus(0);
		orderInstock.setType(1);
		orderInstock.setlType(0);
		orderInstock.setSid(orderRec.getSid());
		orderInstock.setModifTime(now);
		orderInstock.setDeliveryDate(now);
		orderInstock.setValdateDate(now);
		orderInstock.setRate(0.17);
		orderInstock.setOrderIds(orderRec.getOrderId()+"");
		orderInstock.setfStatus(0);
		orderInstock.setOrderMoney1(orderRec.getPrice());
		orderInstock.setOrderMoney5(0.0);
		int result1=mOrderInstockMapper.createNewOrder(orderInstock);//入库单创建
		brs.setCode(result*result1);
		OrderInstockDetail orderInstockDetail = new OrderInstockDetail();
		for(OrderRecDetail ff:fromJson){
			orderInstockDetail.setOrderId(orderInstock.getOrderId());
			orderInstockDetail.setgId(ff.getId());
			orderInstockDetail.setNum(1);
			orderInstockDetail.setInId(orderInstock.getInId());
			orderInstockDetail.setoIndate(now);
			orderInstockDetail.setWeight(0.0+ff.getrNum());
			mOrderInstockDetailMapper.createNewOrderDetail(orderInstockDetail);
		}
		return brs;
	}
	/**
	 * 后台创建的回收订单扫码获取客户
	 */
	@Override
	public BaseRes<WebUser> getCustomerByBarcode(String barcode, String cname,String addr) {
		// TODO Auto-generated method stub
		int flag = 0;
		BaseRes<WebUser> brs = new BaseRes<WebUser>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("barcode", barcode);
		map.put("cname", cname);
		WebUser customerByBarcode = mWebUserMapper.getCustomerByBarcode(map);
		brs.setData(customerByBarcode);
		//System.out.println(customerByBarcode.toString());
		if(addr!=null){
			String addr1 = addr.replace(" ", "");
			String addr2 = customerByBarcode.getUseraddr().replaceAll("[_\\^]", "");
			String[] addr3 = addr2.split("\\|");
			for(int i=0;i<addr3.length;i++){
				if(addr1.equals(addr3[i])){
					flag = 1;
				}
			}
		}
		brs.setCode(flag);
		return brs;
	}
	/**
	 * 手持创建回收订单（有账号的客户）
	 */
	@Override
	public BaseRes<OrderRec> createCompanyOrderRec(String orderRecStr,
			String recCategoryDetailListStr) {
		BaseRes<OrderRec> brs = new BaseRes<OrderRec>();
		Date now = new Date();
		Gson gson = new Gson();
		OrderRec orderRec = gson.fromJson(orderRecStr, OrderRec.class);
		System.out.println(orderRec.getoAuthor());
		ConfigCustomerExt customerIdByUsername = mConfigCustomerExtMapper.getCustomerIdByUsername(orderRec.getoAuthor());
		Type listType = new TypeToken<List<OrderRecDetail>>(){}.getType();
		List<OrderRecDetail> fromJson = gson.fromJson(recCategoryDetailListStr,listType);
		int inId = mOrderRecMapper.findMaxId()+1;
		long orderId = mOrderRecMapper.findMaxOrderId()+1;
		orderRec.setId(inId);
		orderRec.setOrderId(orderId);
		System.out.println(customerIdByUsername.toString());
		orderRec.setCid(customerIdByUsername.getCid());
		orderRec.setCextId(customerIdByUsername.getPid());
		orderRec.setAddr(customerIdByUsername.getAddr());
		orderRec.setStype(1);
		double totalPrice = 0;
		for(OrderRecDetail ff:fromJson){
			totalPrice += ff.getfPrice();
		}
		orderRec.setPrice(totalPrice);
		orderRec.setoStatus(4);
		orderRec.setoIndate(now);
		orderRec.setfStatus(1);
		orderRec.setfJsTime(now);
		orderRec.setfJAuthor(orderRec.getoAuthor());
		orderRec.setfJMoney(totalPrice);
		orderRec.setOrderMoney(totalPrice);
		orderRec.setfJTime(now);
		orderRec.setlCTime(now);
		orderRec.setlAuthor(orderRec.getoAuthor());
		orderRec.setCommitType(1);
		orderRec.setsType(2);
		orderRec.setrDate(now);
		orderRec.setRperson(orderRec.getoAuthor());
		orderRec.setModifTime(now);
		orderRec.setIsRecComplete(1);
		int result = mOrderRecMapper.createOrderRec(orderRec);
		for(OrderRecDetail ff:fromJson){
			ff.setOrderId((int)orderId);
			mOrderRecDetailMapper.createRecDetail(ff);
		}
		OrderInstock orderInstock = new OrderInstock();
		int findMaxInId = mOrderInstockMapper.findMaxInId()+1;
		int findMaxOrderId = mOrderInstockMapper.findMaxOrderId()+1;
		orderInstock.setInId(findMaxInId);
		orderInstock.setOrderId(findMaxOrderId);
		orderInstock.setoIndate(now);
		orderInstock.setCreater(orderRec.getoAuthor());
		orderInstock.setoStatus(0);
		orderInstock.setType(1);
		orderInstock.setlType(0);
		orderInstock.setSid(orderRec.getSid());
		orderInstock.setModifTime(now);
		orderInstock.setDeliveryDate(now);
		orderInstock.setValdateDate(now);
		orderInstock.setRate(0.17);
		orderInstock.setOrderIds(orderRec.getOrderId()+"");
		orderInstock.setfStatus(0);
		orderInstock.setOrderMoney1(orderRec.getPrice());
		orderInstock.setOrderMoney5(0.0);
		int result1=mOrderInstockMapper.createNewOrder(orderInstock);//入库单创建
		brs.setCode(result*result1);
		OrderInstockDetail orderInstockDetail = new OrderInstockDetail();
		for(OrderRecDetail ff:fromJson){
			orderInstockDetail.setOrderId(orderInstock.getOrderId());
			orderInstockDetail.setgId(ff.getId());
			orderInstockDetail.setNum(1);
			orderInstockDetail.setInId(orderInstock.getInId());
			orderInstockDetail.setoIndate(now);
			orderInstockDetail.setWeight(0.0+ff.getrNum());
			mOrderInstockDetailMapper.createNewOrderDetail(orderInstockDetail);
		}
		return brs;
	}
	/**
	 * 后台创建的企业回收订单的完成
	 */
	@Override
	public BaseRes<OrderRec> checkCompanyBackstageOrderRec(String orderRecStr,
			String recCategoryDetailListStr) {
		BaseRes<OrderRec> brs = new BaseRes<OrderRec>();
		Map<String,Object> map = new HashMap<String,Object>();
		Date now = new Date();
		map.put("nowTime", now);
		Gson gson = new Gson();
		OrderRec orderRec = gson.fromJson(orderRecStr, OrderRec.class);
		map.put("orderId", orderRec.getOrderId());
		mBasServiceMapper.recComplete(map);
		System.out.println(orderRec.toString());
		Type listType = new TypeToken<List<OrderRecDetail>>(){}.getType();
		List<OrderRecDetail> fromJson = gson.fromJson(recCategoryDetailListStr,listType);
		orderRec.setStype(1);
		double totalPrice = 0;
		for(OrderRecDetail ff:fromJson){
			totalPrice += ff.getfPrice();
		}
		orderRec.setPrice(totalPrice);
		orderRec.setoStatus(4);
		orderRec.setfStatus(1);
		orderRec.setfJsTime(now);
		orderRec.setlStatus(2);
		orderRec.setlCTime(now);
		orderRec.setModifTime(now);
		int result = mOrderRecMapper.checkCompanyBackstageOrderRec(orderRec);
		long detailOrderId = orderRec.getOrderId();
		for(OrderRecDetail ff:fromJson){
			ff.setOrderId((int)detailOrderId);
			int rt = mOrderRecDetailMapper.createRecDetail(ff);
			result *= rt;
		}
		OrderInstock orderInstock = new OrderInstock();
		int findMaxInId = mOrderInstockMapper.findMaxInId()+1;
		int findMaxOrderId = mOrderInstockMapper.findMaxOrderId()+1;
		orderInstock.setInId(findMaxInId);
		orderInstock.setOrderId(findMaxOrderId);
		orderInstock.setoIndate(now);
		orderInstock.setCreater(orderRec.getfJAuthor());
		orderInstock.setoStatus(0);
		orderInstock.setType(1);
		orderInstock.setlType(0);
		orderInstock.setSid(orderRec.getSid());
		orderInstock.setModifTime(now);
		orderInstock.setValdateDate(now);
		orderInstock.setDeliveryDate(now);
		orderInstock.setRate(0.17);
		orderInstock.setOrderIds(orderRec.getOrderId()+"");
		orderInstock.setfStatus(0);
		orderInstock.setOrderMoney1(orderRec.getPrice());
		orderInstock.setOrderMoney5(0.0);
		int result1=mOrderInstockMapper.createNewOrder(orderInstock);//入库单创建
		brs.setCode(result*result1);
		OrderInstockDetail orderInstockDetail = new OrderInstockDetail();
		for(OrderRecDetail ff:fromJson){
			orderInstockDetail.setOrderId(orderInstock.getOrderId());
			orderInstockDetail.setgId(ff.getId());
			orderInstockDetail.setNum(1);
			orderInstockDetail.setInId(orderInstock.getInId());
			orderInstockDetail.setoIndate(now);
			orderInstockDetail.setWeight(0.0+ff.getrNum());
			mOrderInstockDetailMapper.createNewOrderDetail(orderInstockDetail);
		}
		return brs;
	}
}
